package com.jixu.streetapp.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jixu.streetapp.entity.Street;
import com.jixu.streetapp.mapper.StreetMapper;

import com.jixu.streetapp.service.StreetService;
import com.jixu.streetapp.utils.GetLocationStr;
import com.jixu.streetapp.utils.GetStreetStr;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author "Jixu" 664480310@qq.com
 * @since 1.0.0 2023-02-20
 */
@Service
@Slf4j
public class StreetServiceImpl extends ServiceImpl<StreetMapper, Street> implements StreetService {
    @Autowired
    private StreetMapper mapper;
    @Override
    public List<Street> importExcel(MultipartFile file) {
        ArrayList<Street> list = new ArrayList<>();
        String addressInfo = ""; // 地址信息参数
        String lola = "";//  经纬度信息参数
        InputStream inputStream = null;

        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        XSSFWorkbook Workbook = null;
        try {
            Workbook = new XSSFWorkbook(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        XSSFSheet sheet = Workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
            XSSFRow row = sheet.getRow(rowNum);
            if (row != null) {
                Street street = new Street();
                //使用了getStringCellValue()方法来获取值，POI会判断单元格的类型，如果非字符串类型就会抛出上面的异常。
                //所以先使用setCellType()方法先将该单元格的类型设置为STRING
                //然后poi会根据字符串读取它
                street.setName(row.getCell(0).getStringCellValue());
                street.setAddress(row.getCell(1).getStringCellValue());
                addressInfo = row.getCell(1).getStringCellValue();
                // 发送请求获取字符串 获取经纬度
                String LocationStr = GetLocationStr.doGet(addressInfo);
                // 发送请求   获取街道信息
                String streetInfo = GetStreetStr.doGet(LocationStr);
                // 截取字符串 获得经纬度
                String[] split = LocationStr.split(",");
                String longitude = "";
                String latitude = "";
                if (!split[0].isEmpty()) {
                    longitude = split[0];
                }
                if (!split[1].isEmpty()) {
                    latitude = split[1];
                }
                // 更新街道信息
                street.setLongitude(new BigDecimal(longitude));
                street.setLatitude(new BigDecimal(latitude));
                street.setStreet(streetInfo);
                list.add(street);
                log.info("街道信息 = {}", street.toString());
            }

        }
        // 清空数据库
        this.remove(null);
        // 保存数据
        mapper.saveList(list);
        return list;
    }
}
